﻿using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace SPPropertyBagViewer.Helpers
{
     static class PropertyBagHelper
     {
         #region Getting properties
         public static Hashtable GetPropertiesForWeb(SPWeb web)
         {
             return web.AllProperties;
         }

        public static Hashtable GetPropertiesForList(SPWeb web, Guid listGuid)
        {
            var list = web.Lists.GetList(listGuid, true);
            return list.RootFolder.Properties;
        }

        public static Hashtable GetPropertiesForItem(SPWeb web, Guid listGuid, int itemId)
        {
            var list = web.Lists.GetList(listGuid, true);
            var item = list.GetItemById(itemId);
            return item.Properties;
        }
        #endregion

         public static void UpdateWebPropertyByKey(SPWeb spWeb,  KeyValuePair<string, string> property)
         {
             spWeb.RunAsElevated(currentWeb =>
             {
                 if (!currentWeb.AllProperties.ContainsKey(property.Key))
                 {
                     currentWeb.Properties.Add(property.Key, property.Value);
                 }
                 else
                 {
                     currentWeb.AllProperties[property.Key] = property.Value;
                 }
             });        
         }

         public static void SetPropertyValue(this SPList spList, string key, string value)
         {
             if(string.IsNullOrEmpty(key)) return;
             spList.RunAsElevated((list) =>
             {
                 var rFolder = list.RootFolder;
                 if (!rFolder.Properties.ContainsKey(key))
                 {
                     rFolder.Properties.Add(key, value);
                 }
                 else
                 {
                     rFolder.Properties[key] = value;
                 }
                 rFolder.Update();
             });
         }

         public static string GetPropertyValue(this SPList spList, string key)
         {
             if (string.IsNullOrEmpty(key)) return string.Empty;
             string result = string.Empty;
             spList.RunAsElevated((list) =>
             {
                 var rFolder = list.RootFolder;
                 if (rFolder.Properties.ContainsKey(key))
                     result = rFolder.GetProperty(key).ToString();
             });
             return result;
         }


         public static void DeleteProperty (SPWeb spWeb, KeyValuePair<string, string> property)
         {
             spWeb.RunAsElevated(currentWeb =>
             {
                 if (!currentWeb.AllProperties.ContainsKey(property.Key))
                 {
                     currentWeb.Properties.Remove(property.Key);
                 }
                 else
                 {
                     //TODO if property has been deleted befor
                 }
             });
         }
    }
}
